const db = require('../db');

const login = (req, res) => {
    const { username, password } = req.body;
    // 验证用户名和密码
    const query = `SELECT * FROM users WHERE username = '${username}' AND password = '${password}';`;
    db.query(query, (error, results) => {
      if (error) {
        console.error('Error during login:', error);
        res.status(500).json({ error: 'Internal Server Error' });
      } else {
        if (results.length === 1) {
          // 返回登录成功结果
          res.json({ message: 'Login successful' });
        } else {
          // 返回登录失败结果
          res.status(401).json({ error: 'Invalid credentials' });
        }
      }
    });
  };
  
  module.exports = {
    login,
  };